package com.io.nio;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

/**
 * 视图缓冲器
 * 
 * @author Administrator
 *
 */
public class ViewBuffer {

	public static void main(String[] args) {
		ByteBuffer bb = ByteBuffer
				.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' });
		bb.rewind();
		System.out.print("Byte Buffer ");
		while (bb.hasRemaining()) {
			System.out.print(bb.position() + "-->" + bb.get() + " ");
		}
		System.out.println();

		// 1个char占2个字节
		CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer();
		System.out.print("Char Buffer ");
		while (cb.hasRemaining()) {
			System.out.print(cb.position() + "-->" + cb.get() + " ");
		}
		System.out.println();

		ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
		System.out.print("Short Buffer ");
		while (sb.hasRemaining()) {
			System.out.print(sb.position() + "-->" + sb.get() + " ");
		}
		System.out.println();

		IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer();
		System.out.print("Short Buffer ");
		while (ib.hasRemaining()) {
			System.out.print(ib.position() + "-->" + ib.get() + " ");
		}
		System.out.println();

		LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
		System.out.print("Short Buffer ");
		while (lb.hasRemaining()) {
			System.out.print(lb.position() + "-->" + lb.get() + " ");
		}
		System.out.println();
	}

}
